'*******************************************************************************
' *  
' *  Product: GSALib
' *  Description: A C# API for accessing the Google Search Appliance.
' *
' *  (c) Copyright 2008 Michael Cizmar + Associates Ltd.  (MC+A)
' *  
'*******************************************************************************


Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text

Namespace GSALib.Utils
    ''' <summary>
    ''' Class provides Methods to set Filter in GAS Query
    ''' <para>Author Albert Ghukasyan</para>    
    ''' </summary>
    Public NotInheritable Class QueryFilter
#Region "Variables"

        Private paramSet As New ArrayList()

#End Region

#Region "Contructor"

        Public Sub New(ByVal retainedParams As String())
            Dim i As Integer = 0, iSize As Integer = retainedParams.Length
            While i < iSize
                paramSet.Add(retainedParams(i))
                i += 1
            End While
        End Sub

#End Region

#Region "Methods"

        Public Function filter(ByVal queryString As String) As String
            Dim sbuf As New StringBuilder()
            Dim parameters As String() = queryString.Split("&"c)
            Dim firstTime As Boolean = True
            Dim i As Integer = 0, iSize As Integer = parameters.Length
            While i < iSize
                If parameters(i) IsNot Nothing Then
                    Dim keyValue As String() = parameters(i).Split("="c)
                    If paramSet.Contains(keyValue(0)) Then
                        If Not firstTime Then
                            sbuf.Append("&"c)
                        End If
                        sbuf.Append(keyValue(0)).Append("="c).Append(keyValue(1))
                    End If
                End If
                i += 1
                firstTime = False
            End While

            Return sbuf.ToString()
        End Function

#End Region
    End Class
End Namespace
